package com.zy.openchat.core.model.audio;

import android.os.Parcel;
import android.os.Parcelable;

import com.zy.openchat.Constants;
import com.zy.openchat.core.model.file.FileInfo;

import org.apache.commons.lang3.StringUtils;

import java.io.File;

public class AudioInfo implements Parcelable {
    public int duration;
    public FileInfo audio;
    //file_name : "DDD5669709F4450BB8F88A2DF910558D.wav"
    public String file_name;

    public void updateFile(FileInfo file) {
        if (audio != null&&file!=null&&audio.id == file.id) {
            audio = file;
        }
    }

    public String getRemoteFileId(){
        if(audio!=null&&audio.remote!=null){
            return audio.remote.id;
        }
        return "";
    }

    public long totalSize() {
        if (audio != null)
            return audio.expected_size;
        return 0;
    }

    public long donwloadSize() {
        if (audio != null && audio.local != null)
            return audio.local.downloaded_size;
        return 0;
    }

    public String localAudioPath() {
        if (audio != null && audio.local != null && audio.local.isExist()) {
            return audio.local.path;
        }
        //本地路径，根据file_name获取
        if (!StringUtils.isEmpty(file_name)) {
            try {
                String path = Constants.userAudioPath() + file_name;
                File f = new File(path);
                if (f.exists()) {
                    return path;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public boolean isAudioDownloaded() {
        if (audio != null && audio.local != null && audio.local.isExist()) {
            return true;
        }
        //本地路径，根据file_name判断
        if (!StringUtils.isEmpty(file_name)) {
            try {
                String path = Constants.userAudioPath() + file_name;
                File f = new File(path);
                if (f.exists()) {
                    return true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.duration);
        dest.writeParcelable(this.audio, flags);
        dest.writeString(this.file_name);
    }

    public AudioInfo() {
    }

    protected AudioInfo(Parcel in) {
        this.duration = in.readInt();
        this.audio = in.readParcelable(FileInfo.class.getClassLoader());
        this.file_name = in.readString();
    }

    public static final Creator<AudioInfo> CREATOR = new Creator<AudioInfo>() {
        @Override
        public AudioInfo createFromParcel(Parcel source) {
            return new AudioInfo(source);
        }

        @Override
        public AudioInfo[] newArray(int size) {
            return new AudioInfo[size];
        }
    };
}
